package com.grts.mall.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.grts.mall.mapper.CartMapper;
import com.grts.mall.pojo.Cart;
import com.grts.mall.pojo.CartKey;
import com.grts.mall.pojo.vo.CartResultVo;

@Service
public class CartServiceImpl implements CartService {

	@Autowired
	private CartMapper cartMapper;
	
	
	@Override
	public void add(Cart cart) {
		CartKey key = new CartKey();
		key.setPid(cart.getPid());
		key.setUid(cart.getUid());
		Cart c = this.cartMapper.selectByPrimaryKey(key);
		
		if (c != null) {
			System.out.println("进入更新");
			c.setQuantity(cart.getQuantity()+c.getQuantity());
			this.cartMapper.updateByPrimaryKey(c);

		} else {
			this.cartMapper.insert(cart);
		}
	}

	@Override
	public List<CartResultVo> findByCart(Long uid) {
		List<CartResultVo> carts = this.cartMapper.findByCart(uid);
		return carts;
	}

	@Override
	public void updata(Cart cart) {
		this.cartMapper.updateByPrimaryKey(cart);
	}

	@Override
	public Long del(Cart cart) {
		CartKey key = new CartKey();
		key.setPid(cart.getPid());
		key.setUid(cart.getUid());
		int ok = this.cartMapper.deleteByPrimaryKey(key);
		return (long) ok;
	}
}